<?php
declare (strict_types = 1);

namespace app\admin\controller;
use app\admin\model\UserModel;
use app\admin\model\RoleModel;
class User extends Base
{
	/**
	 * 用户列表
	 */
	public function index(){
		if(request()->isPost()){
            $page=empty(request()->post('page/d'))?1:request()->post('page/d');
            $limit=empty(request()->post('limit/d'))?10:request()->post('limit/d');
            if(empty($limit) || $limit>100){
                $limit=100;
            }
            $list=UserModel::with(['role'])
            	->limit(($page-1)*$limit,$limit)
                ->select();
            $count=UserModel::count('id');
            return $this->showList($list,$count);
        }    
        return $this->fetch();
	}
	/**
	 * 用户添加
	 */
	public function add(){
		$info['action']="添加";
        if(request()->isPost()){
            $data=request()->post();
            try {
                $this->validate($data,'User');
            } catch (\Exception $e) {
                return $this->error($e->getMessage());
            }

            if(empty($data['password'])){
            	return $this->error('密码不能为空');
            }
            $data['password']=password_encrypt($data['password']);
            if($data['password']===false){
            	return $this->error('添加失败');
            }
            $data['status']=(isset($data['status']) && $data['status']=="on")?1:0;
            if(UserModel::create($data)){
                return $this->success('操作成功',(string)url('index'));
            }
            return $this->error('添加失败');
        }
        $roleList=RoleModel::select();
        $this->assign(['info'=>$info,'roleList'=>$roleList]);
        return $this->fetch();
	}
	/**
	 * 用户编辑
	 */
	public function edit(){
		$id=request()->get('id/d');
        if(!empty($id)){
            $info=UserModel::find($id);
            if(empty($info)){
                return $this->error('信息不存在');
            }
            $info=$info->toArray();
            $info['action']='修改';
        }else{
            return $this->error('信息不存在');
        }

        if(request()->isPost()){
            $data=request()->post();
            try {
                $this->validate($data,'User');
            } catch (\Exception $e) {
                return $this->error($e->getMessage());
            }
            if(!empty($data['password'])){
            	$data['password']=password_encrypt($data['password']);
	            if($data['password']===false){
	            	return $this->error('添加失败');
	            }
            }else{
                unset($data['password']);
            }
            $data['status']=(isset($data['status']) && $data['status']=="on")?1:0;
            if(UserModel::update($data,['id'=>$id])){
                return $this->success('操作成功',(string)url('index'));
            }
            return $this->error('修改失败');
        }
		$roleList=RoleModel::select();
        $this->assign(['info'=>$info,'roleList'=>$roleList]);
        return $this->fetch('add');
	}
	/**
	 * 用户删除
	 */
	public function del(){
		$id=request()->post('id/d');

		if(!empty($id) && $id==$this->admin['id']){
			return $this->error('不能删除自己');
		}
		if(UserModel::destroy($id)){
			return $this->success('删除成功');
		}else{
			return $this->error('删除失败');
		}
	}

    /**
     * 修改密码
     */

    public function editpassword(){
        if(request()->isPost()){
            $data=request()->post();
            try {
                $this->validate($data,[
                    'old_password'=>['require','min'=>6,'max'=>32,'alphaDash'],
                    'new_password'=>['require','min'=>6,'max'=>32,'alphaDash'],
                    'again_password'=>['require','min'=>6,'max'=>32,'alphaDash'],
                ]);
            } catch (\Exception $e) {
                return $this->error('密码长度6-32,并且只能为字母和数字,下划线_');
            }

            if($data['new_password']!==$data['again_password']){
                return $this->error('两次输入密码不一致');
            }
            $mypassword=UserModel::where(['id'=>$this->admin['id']])->value('password');

            if(!password_verifymy($data['old_password'],$mypassword)){
                return $this->error('旧密码错误');
            }

            UserModel::where(['id'=>$this->admin['id']])->update(['password'=>password_encrypt($data['new_password'])]);
            return $this->success('修改成功');
        }
        return $this->fetch();
    }
    
}
